﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web.Mvc;
using Smarly.Bussines.Membership;
using Smarly.Bussines.Model;
using Smarly.Core;
using Smarly.Web.ActionFilters;
using Smarly.Web.Controllers.Base;
using log4net;

namespace Smarly.Web.Controllers
{
	public class HomeController : BaseController
	{
		#region redirect urls

		private static readonly Dictionary<string, string> redirectUrls = new Dictionary<string, string>();

		static HomeController()
		{
			redirectUrls.Add("pro-jquery-znakomstvo-jquery", "/pro-jquery/pro-jquery-vvedenie/pro-jquery-znakomstvo-jquery");
			redirectUrls.Add("pro-jquery-osnovi-html", "/pro-jquery/pro-jquery-vvedenie/pro-jquery-osnovi-html");
			redirectUrls.Add("pro-jquery-osnovi-css", "/pro-jquery/pro-jquery-vvedenie/pro-jquery-osnovi-css");
			redirectUrls.Add("pro-jquery-osnovi-java-script", "/pro-jquery/pro-jquery-vvedenie/pro-jquery-osnovi-java-script");
			redirectUrls.Add("pro-jquery-osnovi-jquery", "/pro-jquery/pro-jquery-basic-jquery/pro-jquery-osnovi-jquery");
			redirectUrls.Add("pro-jquery-manage", "/pro-jquery/pro-jquery-basic-jquery/pro-jquery-manage");
			redirectUrls.Add("pro-jquery-manipulating-dom", "/pro-jquery/pro-jquery-basic-jquery/pro-jquery-manipulating-dom");
			redirectUrls.Add("pro-jquery-manipulating-elements", "/pro-jquery/pro-jquery-basic-jquery/pro-jquery-manipulating-elements");
			redirectUrls.Add("pro-jquery-working-with-events", "/pro-jquery/pro-jquery-basic-jquery/pro-jquery-working-with-events");
			redirectUrls.Add("pro-jquery-using-jquery-effects", "/pro-jquery/pro-jquery-basic-jquery/pro-jquery-using-jquery-effects");
			redirectUrls.Add("pro-jquery-refactoring-the-example-part-i", "/pro-jquery/pro-jquery-basic-jquery/pro-jquery-refactoring-the-example-part-i");
			redirectUrls.Add("pro-jquery-using-data-templates", "/pro-jquery/pro-jquery-data-and-ajax/pro-jquery-using-data-templates");
			redirectUrls.Add("pro-jquery-working-with-forms", "/pro-jquery/pro-jquery-data-and-ajax/pro-jquery-working-with-forms");
			redirectUrls.Add("pro-jquery-using-ajax-i", "/pro-jquery/pro-jquery-data-and-ajax/pro-jquery-using-ajax-i");
			redirectUrls.Add("pro-jquery-using-ajax-ii", "/pro-jquery/pro-jquery-data-and-ajax/pro-jquery-using-ajax-ii");
			redirectUrls.Add("pro-jquery-refactoring-the-example-part-ii", "/pro-jquery/pro-jquery-data-and-ajax/pro-jquery-refactoring-the-example-part-ii");

			redirectUrls.Add("html5-putting-html5-in-context", "/html5/htlm5-getting-started/html5-putting-html5-in-context");
			redirectUrls.Add("html5-getting-ready", "/html5/htlm5-getting-started/html5-getting-ready");
			redirectUrls.Add("html5-getting-started-with-html", "/html5/htlm5-getting-started/html5-getting-started-with-html");
			redirectUrls.Add("html5-getting-started-with-css", "/html5/htlm5-getting-started/html5-getting-started-with-css");
			redirectUrls.Add("html5-getting-started-with-javascript", "/html5/htlm5-getting-started/html5-getting-started-with-javascript");

			redirectUrls.Add("what-is-the-big-idea", "/pro-asp-net-mvc-4/introducing-asp-net-mvc-4/what-is-the-big-idea");
			redirectUrls.Add("your-first-mvc-application", "/pro-asp-net-mvc-4/introducing-asp-net-mvc-4/your-first-mvc-application");
			redirectUrls.Add("mvc-pattern", "/pro-asp-net-mvc-4/introducing-asp-net-mvc-4/mvc-pattern");

			redirectUrls.Add("pro-asp-net-mvc-4/introducing-asp-net-mvc-4/what-is-the-big-idea/what’s-new-in-asp-net-mvc-4", "/pro-asp-net-mvc-4/introducing-asp-net-mvc-4/what-is-the-big-idea/what-is-new-in-asp-net-mvc-4");
			redirectUrls.Add("pro-asp-net-mvc-4/introducing-asp-net-mvc-4/what-is-the-big-idea/whatâ€™s-new-in-asp-net-mvc-4", "/pro-asp-net-mvc-4/introducing-asp-net-mvc-4/what-is-the-big-idea/what-is-new-in-asp-net-mvc-4");
		}

		#endregion

		public ActionResult Index()
		{
			IPrincipal principal = User;
			List<Book> model = new Book().Get((SmarlyIdentity)principal.Identity);

			if (!principal.IsInRole("Admin") && !principal.IsInRole("UnpublishedViewer"))
			{
				model = model.Where(x => x.Root.Published).ToList();
			}

			return View("Index", model);
		}


		[BookStatistic("parts")]
		public ActionResult Text(string parts)
		{
			if (parts.Contains("."))
			{
				throw new UnknownPageException();
			}

			if (redirectUrls.ContainsKey(parts))
				return RedirectPermanent(redirectUrls[parts]);


			var p = (parts ?? string.Empty).Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);

			var mapUrl = p.Aggregate("", (current, part) => current + ("/" + part));

			BookTree model = new BookTree().Get((SmarlyIdentity)User.Identity, mapUrl);

			ViewBag.IsAdmin = User.IsInRole("Admin");
			ViewBag.IsAllowEdit = ViewBag.IsAdmin || (!model.Published && ((SmarlyPrincipal)User).AllowChanges(model.Url));
			ViewBag.ShowUnpublishContent = ViewBag.IsAllowEdit || User.IsInRole("UnpublishedViewer");
			ViewBag.ShowContent = true;//ViewBag.ShowUnpublishContent || User.IsInRole("Viewer");

			return View(model);
		}


		public ActionResult About()
		{
			throw new ApplicationException("About my except");
		}

	}
}
